//
//  DGBlockAlertView.m
//  DGUIKit
//
//  Created by Jinxiao on 6/9/13.
//  Copyright (c) 2013 debugeek. All rights reserved.
//

#import "DGBlockAlertView.h"

@interface DGBlockAlertView () <UIAlertViewDelegate>

@end

@implementation DGBlockAlertView

+ (instancetype)alertWithTitle:(NSString *)title message:(NSString *)message cancelTitle:(NSString *)cancelTitle cancelBlock:(void (^)(void))cancelBlock confirmTitle:(NSString *)confirmTitle comfirmBlock:(void (^)(void))comfirmBlock
{
    DGBlockAlertView *alertView = [[DGBlockAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:cancelTitle otherButtonTitles:confirmTitle, nil];

    NSInteger otherButtonIndex = alertView.firstOtherButtonIndex;

    [alertView setDidClickButtonBlock:^(NSInteger index) {
        if(index == otherButtonIndex)
        {
            if(confirmTitle && comfirmBlock)
            {
                comfirmBlock();
            }
        }
        else
        {
            if(cancelBlock)
            {
                cancelBlock();
            }
        }
    }];

    return alertView;
}

#pragma mark - UIAlertView Delegate Methods

+ (void)alertView:(DGBlockAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(alertView.didClickButtonBlock)
    {
        alertView.didClickButtonBlock(buttonIndex);
    }
}

+ (void)alertViewCancel:(DGBlockAlertView *)alertView
{
    if(alertView.didCancelBlock)
    {
        alertView.didCancelBlock();
    }
}

+ (void)willPresentAlertView:(DGBlockAlertView *)alertView
{
    if(alertView.willPresentBlock)
    {
        alertView.willPresentBlock();
    }
}

+ (void)didPresentAlertView:(DGBlockAlertView *)alertView
{
    if(alertView.didPresentBlock)
    {
        alertView.didPresentBlock();
    }
}

+ (void)alertView:(DGBlockAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if(alertView.willDismissWithButtonBlock)
    {
        alertView.willDismissWithButtonBlock(buttonIndex);
    }
}

+ (void)alertView:(DGBlockAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if(alertView.didDismissWithButtonBlock)
    {
        alertView.didDismissWithButtonBlock(buttonIndex);
    }
}

@end
